/*///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

FIVe3D
Flash Interactive Vector-based 3D
Mathieu Badimon  |  five3d@mathieu-badimon.com

http://five3D.mathieu-badimon.com  |  http://five3d.mathieu-badimon.com/archives/  |  http://code.google.com/p/five3d/

/*///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

import net.badimon.five3D.display.DisplayObjectContainer;
import net.badimon.five3D.display.Motif;
import net.badimon.five3D.display.Scene;
import net.badimon.five3D.display.Shape;
import net.badimon.five3D.geom.Angle;
import net.badimon.five3D.typography.HelveticaMedium;

dynamic class net.badimon.five3D.display.DynamicText extends DisplayObjectContainer {

	private var __text:String = "";
	private var __typography:Object;
	private var __size:Number = 10;
	private var __color:Number = 0x000000;

	function DynamicText(scene:Scene, parentclip:MovieClip, name:String, depth:Number, parent:DisplayObjectContainer) {
		super(scene, parentclip, name, depth, parent);
		_typography = HelveticaMedium;
	}

	public function get _text():String {
		return __text;
	}

	public function set _text(value:String):Void {
		createGlyphs(value);
		__text = value;
		removeAdditionalGlyphs();
		placeGlyphs();
		__scene.__render = true;
		__render = true;
	}

	private function createGlyphs(text:String):Void {
		for (var i:Number = 0;i < text.length; i++) if (text.charAt(i) != __text.charAt(i)) createGlyph(i, text.charAt(i));
	}

	private function createGlyph(number:Number, glyph:String):Void {
		__children[number] = new Shape(__scene, _clip, "glyph" + number, number, this);
		__children[number].addMotif([['B', [__color, 100]]].concat(Motif.clone(__typography.__motifs[glyph])).concat([['E']]));
		__children[number]._scalex = __children[number]._scaley = __size;
	}

	private function removeAdditionalGlyphs(Void):Void {
		for (var i:Number = __text.length;i < __children.length; i++) __children[i]._clip.removeMovieClip();
		__children.splice(__text.length);
	}

	private function placeGlyphs(Void):Void {
		var withdrawal:Number = 0;
		for (var i:Number = 0;i < __children.length; i++) {
			if (__children[i]._x != withdrawal) __children[i]._x = withdrawal;
			withdrawal += __typography.__widths[__text.charAt(i)] * __size / 100;
		}
	}

	public function set _typography(object:Object):Void {
		__typography = object;
		if (!__typography.__initialized) __typography.initialize();
		reinitText(__text);
	}

	private function reinitText(text:String):Void {
		__text = "";
		_text = text;
	}

	public function get _size():Number {
		return __size;
	}

	public function set _size(value:Number):Void {
		__size = value;
		resizeGlyphs();
		placeGlyphs();
		__scene.__render = true;
		__render = true;
	}

	private function resizeGlyphs(Void):Void {
		for (var i:Number = 0;i < __children.length; i++) __children[i]._scalex = __children[i]._scaley = __size;
	}

	public function get _color():Number {
		return __color;
	}

	public function set _color(value:Number):Void {
		__color = value;
		colorateGlyphs();
	}

	private function colorateGlyphs(Void):Void {
		for (var i:Number = 0;i < __children.length; i++) (new Color(__children[i]._clip)).setRGB(__color);
	}

	public function removeDynamicText(Void):Void {
		delete _parent[_name];
		delete _parent.__children[_name];
		_clip.removeMovieClip();
	}

	public function render(Void):Void {
		if (!__visible && _clip._visible) _clip._visible = false;
		else if (__visible) {
			if (!_clip._visible) _clip._visible = true;
			if (__render) {
				getMatrices();
				__render = false;
			}
			for (var i:String in __children) __children[i].render();
		}
	}

	private function getMatrices(Void):Void {
		__matrix.createBox(__scalex / 100, __scaley / 100, __scalez / 100, Angle.degreesToRadians(__rotationx), Angle.degreesToRadians(__rotationy), Angle.degreesToRadians(__rotationz), __x, __y, __z);
		__concatenatedmatrix = _parent.__concatenatedmatrix.clone();
		__concatenatedmatrix.concat(__matrix);
	}
}